<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class AutogeneratorControllerXml_element extends JControllerForm {

    protected $oApplication;

    /**
     *
     * @var AutogeneratorModelXml_element 
     */
    public $oModelXml_element;

    public function __construct($aConfig = array()) {
        parent::__construct($aConfig);

        $this->oApplication = JFactory::getApplication();

        $this->oModelXml_element = $this->getModel();
    }

    public function cancel($key = null) {
        // Check for request forgeries.
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=xml_types', false));

        return true;
    }

    public function add() {
        $iXmlTypeId = $this->input->getInt('iXmlTypeId');

        $sPath = 'index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=add&iXmlTypeId=' . $iXmlTypeId;

        $this->setRedirect(JRoute::_($sPath, false));
    }

    public function delete() {
        $iXmlTypeId = $this->input->getInt('iXmlTypeId');

        $sPath = 'index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=delete&iXmlTypeId=' . $iXmlTypeId;

        $this->setRedirect(JRoute::_($sPath, false));
    }

    public function edit($key = null, $urlVar = null) {
        $app = JFactory::getApplication();

        $model = $this->getModel();
        $table = $model->getTable();

        $cid = $this->input->post->get('cid', array(), 'array');

        $context = "$this->option.edit.$this->context";

        // Determine the name of the primary key for the data.
        if (empty($key)) {
            $key = $table->getKeyName();
        }

        // To avoid data collisions the urlVar may be different from the primary key.
        if (empty($urlVar)) {
            $urlVar = $key;
        }

        // Get the previous record id (if any) and the current record id.
        $recordId = (int) (count($cid) ? $cid[0] : $this->input->getInt($urlVar));

        $checkin = property_exists($table, 'checked_out');

        // Attempt to check-out the new record for editing and redirect.
        if ($checkin && !$model->checkout($recordId)) {
            // Check-out failed, display a notice but allow the user to see the record.
            $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()));
            $this->setMessage($this->getError(), 'error');

            $this->setRedirect(
                    JRoute::_(
                            'index.php?option=' . $this->option . '&view=' . $this->view_item
                            . $this->getRedirectToItemAppend($recordId, $urlVar), false
                    )
            );

            return false;
        } else {
            // Check-out succeeded, push the new record id into the session.
            $this->holdEditId($context, $recordId);

            $app->setUserState($context . '.data', null);

            $sPath = 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar);

            $this->setRedirect(JRoute::_($sPath, false));

            return true;
        }
    }

    public function editInline() {
        $aData = $this->oApplication->input->get('data', array(), 'array');
        
        if (!$this->oModelXml_element->editInline($aData)) {
            echo JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $this->oModelXml_element->getError());
        }
        echo 1;

        die;
    }

    public function save($key = null, $urlVar = null) {
        // Check for request forgeries.
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $this->oApplication = JFactory::getApplication();

        $oTable = $this->oModelXml_element->getTable();
        $oLang = JFactory::getLanguage();

        $aData = $this->input->post->get('jform', array(), 'array');
        $aDefault = $this->input->post->get('aDefault', array(), 'array');
        $sTask = $this->getTask();

        switch ($sTask) {
            case 'apply':
                $sPath = 'index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=add&iXmlTypeId=' . $aData['xml_type_id'];

                if (count($aData['xml_attribute_list']) > 0) {
                    foreach ($aData['xml_attribute_list'] as $iXmlAttributeId) {
                        $aXmlElementData = array();
                        $aXmlElementData['xml_type_id'] = $aData['xml_type_id'];
                        $aXmlElementData['xml_attribute_id'] = $iXmlAttributeId;
                        $aXmlElementData['custom'] = $aDefault[$iXmlAttributeId];

                        if (!$this->oModelXml_element->add($aXmlElementData)) {
                            // Redirect back to the edit screen.
                            $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $this->oModelXml_element->getError()));

                            $this->setMessage($this->getError(), 'error');

                            $this->setRedirect(JRoute::_($sPath, false));

                            return false;
                        }
                    }
                }

                $this->setMessage(JText::_(($oLang->hasKey($this->text_prefix . '_SUBMIT_SAVE_SUCCESS') ? $this->text_prefix : 'JLIB_APPLICATION') . '_SUBMIT_SAVE_SUCCESS'));

                $this->setRedirect(JRoute::_($sPath, false));

                break;

            case 'save':
                if (count($aData['xml_attribute_list']) > 0) {
                    foreach ($aData['xml_attribute_list'] as $iXmlAttributeId) {
                        $aXmlElementData = array();
                        $aXmlElementData['xml_type_id'] = $aData['xml_type_id'];
                        $aXmlElementData['xml_attribute_id'] = $iXmlAttributeId;
                        $aXmlElementData['custom'] = $aDefault[$iXmlAttributeId];

                        if (!$this->oModelXml_element->add($aXmlElementData)) {
                            // Redirect back to the edit screen.
                            $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $this->oModelXml_element->getError()));

                            $this->setMessage($this->getError(), 'error');

                            $sPath = 'index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=add&iXmlTypeId=' . $aData['xml_type_id'];

                            $this->setRedirect(JRoute::_($sPath, false));

                            return false;
                        }
                    }
                }

                $this->setMessage(JText::_(($oLang->hasKey($this->text_prefix . '_SUBMIT_SAVE_SUCCESS') ? $this->text_prefix : 'JLIB_APPLICATION') . '_SUBMIT_SAVE_SUCCESS'));

                $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=xml_types', false));

                break;
        }
    }

}
